import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:flutter_learn_vi/src/bean/bean_mine_uestion.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/page/mine/question_model.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/4.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
class MineItemPage extends StatefulWidget {
  //页面标识
  final int pageIndex;

  //构造函数
  MineItemPage(this.pageIndex, {Key key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return ScffoldHomeItemState();
  }
}

class ScffoldHomeItemState extends State<MineItemPage>
    with AutomaticKeepAliveClientMixin, MineQuestionModel {
  //页面创建时初始化函数
  MineQuestionBean _mineQuestionBean;

  List<MineQuestionBean> _list;

  @override
  void initState() {
    super.initState();
    print("页面创建${widget.pageIndex}");
    _mineQuestionBean = UserHelper.getInstance.selectMineQuestionBean;
    _list = UserHelper.getInstance.questionList;
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    if (_mineQuestionBean == null) {
      return NoDataWidget(
        clickCallBack: () async {
          await getQuestion();
          LogUtils.e("获取数据完成");
          _mineQuestionBean = UserHelper.getInstance.selectMineQuestionBean;
          _list = UserHelper.getInstance.questionList;
          setState(() {});
        },
      );
    }
    return Container(
      color: Colors.white,
      padding: EdgeInsets.all(8),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Divider(),
          SizedBox(
            height: 8,
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                child: Text(
                  "当前题库",
                  style: TextStyle(fontSize: 14),
                ),
              ),
              SizedBox(
                width: 16,
              ),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text("${_mineQuestionBean.questionTitle}"),
                    Container(
                      margin: EdgeInsets.only(top: 16),
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            width: 10,
                            height: 10,
                            margin: EdgeInsets.only(right: 10, top: 4),
                            decoration: BoxDecoration(
                                borderRadius:
                                    BorderRadius.all(Radius.circular(5)),
                                color: Colors.blue),
                          ),
                          Expanded(
                            child:
                                Text("当前题库有 ${_mineQuestionBean.questContent}"),
                          ),
                        ],
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
          Divider(),
          Container(
            padding: EdgeInsets.only(left: 8, right: 8, top: 4, bottom: 4),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.all(Radius.circular(30)),
            ),
            child: Text("可点击以下题库切换使用"),
          ),
          Expanded(
            child: ListView.builder(
              padding: EdgeInsets.zero,
              itemBuilder: (BuildContext context, int index) {
                MineQuestionBean bean = _list[index];

                return GestureDetector(
                  onTap: () async {
                    bool isSelect = await showCupertinoDialog(
                        builder: (BuildContext context) {
                          return CupertinoAlertDialog(
                            title: Text("温馨提示"),
                            content: Container(
                              padding: EdgeInsets.all(10),
                              child: Text("确定选择题库${bean.questionTitle}吗？"),
                            ),
                            actions: [
                              CupertinoDialogAction(
                                child: Text("取消"),
                                onPressed: () {
                                  NavigatorUtils.pop(context,
                                      parameters: false);
                                },
                              ),
                              CupertinoDialogAction(
                                child: Text("确定"),
                                onPressed: () {
                                  NavigatorUtils.pop(context, parameters: true);
                                  _mineQuestionBean = bean;
                                  UserHelper.getInstance
                                      .selectMineQuestionBean = bean;
                                },
                              )
                            ],
                          );
                        },
                        context: context);

                    if (isSelect != null && isSelect) {
                      ToastUtils.showToast("已切换题库");
                      setState(() {});
                    }
                  },
                  child: Container(
                    padding: EdgeInsets.all(8),
                    height: 44,
                    child: Row(
                      children: [
                        Container(
                          width: 2,
                          height: 2,
                          margin: EdgeInsets.only(right: 8),
                          decoration: BoxDecoration(
                              color: Colors.indigoAccent,
                              borderRadius:
                                  BorderRadius.all(Radius.circular(20))),
                        ),
                        Text("${bean.questionTitle}"),
                      ],
                    ),
                  ),
                );
              },
              itemCount: _list.length,
            ),
          ),
        ],
      ),
    );
  }

  //页面销毁时回调函数
  @override
  void dispose() {
    super.dispose();
    print("页面消失${widget.pageIndex}");
  }

  @override
  bool get wantKeepAlive => true;
}
